# Contributor: Fabian Affolter <fabian@affolter-engineering.ch>
# Maintainer: Fabian Affolter <fabian@affolter-engineering.ch>
pkgname=py3-pip
pkgver=23.3.2
pkgrel=0
pkgdesc="Tool for installing and managing Python packages"
url="https://pip.pypa.io"
arch="noarch"
license="MIT"
depends="
	py3-setuptools
	python3
	"
makedepends="
	py3-sphinx
	py3-sphinx-copybutton
	py3-sphinx-inline-tabs
	py3-myst-parser
	"
checkdepends="
	py3-cryptography
	py3-freezegun
	py3-mock
	py3-pretend
	py3-pytest
	py3-yaml
	py3-scripttest
	py3-virtualenv
	py3-werkzeug
	"
subpackages="
	$pkgname-doc
	$pkgname-pyc
	$pkgname-zsh-completion
	$pkgname-bash-completion
	"
source="https://github.com/pypa/pip/archive/$pkgver/pip-$pkgver.tar.gz"
builddir="$srcdir/pip-$pkgver"

provides="py-pip=$pkgver-r$pkgrel" # Backwards compatibility
replaces="py-pip" # Backwards compatibility
options="!check" # check is broken

prepare() {
	default_prepare

	# Remove certifi usage
	sed -i 's|from pip._vendor.certifi import where|where = lambda: "/etc/ssl/certs/ca-certificates.crt"|' src/pip/_internal/commands/debug.py

	# Do not use furo as HTML theme in docs
	# furo is not available in Alpine
	sed -i '/html_theme = "furo"/d' docs/html/conf.py

	# Upstream uses a Python 2/3 compatibility library for csv with Python 3 semantics in tests
	# We only target Python 3 and csv23 is not (yet) packaged
	# As of 20.1b1, this workaround was sufficient to get around the missing dependency
	sed -i 's/csv23/csv/g' tests/lib/wheel.py

	# not packaged (and has another ~5 deps), and not actually used it seems
	# (maybe for html docs)
	sed -i '/sphinxcontrib.towncrier/d' docs/html/conf.py
}

build() {
	python3 setup.py build

	# see noxfile.py
	export PYTHONPATH="$PWD/build/lib"
	sphinx-build -c docs/html -d docs/build/doctrees/man -b man docs/man docs/build/man
}

check() {
	# --deselect'ed and -k'ed tests are not compatible with the latest virtualenv
	# These files contain almost 500 tests so we should enable them back
	# as soon as pip will be compatible upstream
	# https://github.com/pypa/pip/pull/8441
	PYTHONPATH="$PWD/build/lib" pytest -m 'not network' \
		-k "not test_from_link_vcs_with_source_dir_obtains_commit_id and not test_from_link_vcs_without_source_dir and not test_should_cache_git_sha" \
		--deselect tests/functional \
		--deselect tests/lib/test_lib.py \
		--deselect tests/unit/test_build_env.py
}

package() {
	python3 setup.py install --skip-build --root="$pkgdir"

	install -Dm644 docs/build/man/* -t "$pkgdir"/usr/share/man/man1

	local _py3ver=$(python3 -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))')
	PYTHONPATH="$pkgdir"/usr/lib/python$_py3ver/site-packages "$pkgdir"/usr/bin/pip \
		completion --bash | \
		install -Dm644 /dev/stdin "$pkgdir"/usr/share/bash-completion/completions/pip

	PYTHONPATH="$pkgdir"/usr/lib/python$_py3ver/site-packages "$pkgdir"/usr/bin/pip \
		completion --zsh | \
		install -Dm644 /dev/stdin "$pkgdir"/usr/share/zsh/site-functions/_pip
}

sha512sums="
976c6ab9959805f4d20d4bdedb4dabc95dd55f1ec78773f63f738599bbe44e766ad3a1394bee052a2c2386826d67686ad456726373345ba9e0d555fd251dff09  pip-23.3.2.tar.gz
"
